import * as departmentService from '../../services/organize/departmentService';
import * as roleService from '../../services/user/roleService';
import * as authorizeService from '../../services/limit/authorizeService';
import { message } from "antd";
import config from '../../config/config';
import * as correlateService from "../../services/limit/correlateService";
import * as menuService from "../../services/option/menuService";
import * as applicationService from "../../services/organize/applicationService";
import {getAppRoleTree} from "../../services/limit/correlateService";
import {deleteAuthorize, getAuthorizeList} from "../../services/limit/authorizeService";

/**
 * 部门管理Model
 */
export default {
  namespace: "authorizeModel",
  state: {
    bizType: "authorize",
    listLoading: false,
    authorizeData: [],
    total: 0,
    currentPage: 1,
    pageSize: config.PAGE_SIZE,
    departmentModalVisible: false,
    operateType: 'add',
    departmentInfoData: null,
    selectedRowKeys: [],
    searchParams: null,
    limitList: [],
    companyList: [],
    authorizeVos: [],
    authorizeList: [],
    correlateType: "0",
    menuLimitDrawerVisible: false,
    userAllotTransferVisible: false,
    viewAuthorizeDrawerVisible: false,
    allotId: null,
    allUserList: [],
    menuList: [],
    correlateRecord: null,
    correlateSelectedKeys: null,
    authorizeDetailRecord: null,
    parentRecord: null,
    menuDrawerType: "authorize",
  },

  reducers: {
    updateState(state, { payload }) {
      return { ...state, ...payload }
    },
  },

  effects: {
    *initApp({payload: params}, {select, call, put}) {
      yield put({ type: "updateState", payload: { correlateType: "2" }});
      yield put({ type: 'getAppRoleTree', payload: { type: "2" }});
      yield put({ type: 'getAuthorizePage', payload: { type: "2" }});
    },

    *initRole({payload: params}, {select, call, put}) {
      yield put({ type: "updateState", payload: { correlateType: "0" }});
      yield put({ type: 'getAppRoleTree', payload: { type: "6" }});
      yield put({ type: 'getAuthorizePage', payload: { type: "0" }});
    },

    *initPost({payload: params}, {select, call, put}) {
      yield put({ type: "updateState", payload: { correlateType: "1" }});
      yield put({ type: 'getUnitNodeTree', payload: { type: "5" }});
      yield put({ type: 'getAuthorizePage', payload: { type: "1" }});
    },

    *getAuthorizeList({payload: params }, { select, call, put }) {
      const res = yield call(authorizeService.getAuthorizeList, { ...params });
      if (res.code == "200") {
        yield put({ type: 'updateState', payload: { authorizeVos: res.data }});
      }
    },

    *getApplicationList({payload: params }, { select, call, put }) {
      yield put({ type: "updateState", payload: { departmentLoading: true }});
      const res = yield call(applicationService.getApplicationList, { ...params });
      if (res.code == "200") {
        yield put({ type: 'updateState', payload: { limitList: res.data }});
      }
      yield put({ type: "updateState", payload: { departmentLoading: false }});
    },

    *getUnitNodeTree({ payload: params }, { select, call, put }) {
      yield put({ type: "updateState", payload: { departmentLoading: true }});
      let res = yield call(correlateService.getUnitNodeTree, params);
      if (res && res.code == 200) {
        yield put({ type: "updateState", payload: { limitList: res.data }});
      }
      yield put({ type: "updateState", payload: { departmentLoading: false }});
    },

    *getRoleList({payload: params }, { select, call, put }) {
      yield put({ type: "updateState", payload: { departmentLoading: true }});
      const res = yield call(roleService.getRoleList, { ...params });
      if (res.code == "200") {
        yield put({ type: 'updateState', payload: { limitList: res.data }});
      }
      yield put({ type: "updateState", payload: { departmentLoading: false }});
    },

    *getAppRoleTree({payload: params }, { select, call, put }) {
      const res = yield call(correlateService.getAppRoleTree, { ...params });
      if (res.code == "200") {
        yield put({ type: 'updateState', payload: { limitList: res.data }});
      }
    },

    *getAuthorizePage({payload: params}, { select, call, put }) {
      yield put({ type: "updateState", payload: { listLoading: true }});
      const { currentPage, pageSize } = yield select(state => state.authorizeModel);
      let res = yield call(authorizeService.getAuthorizePage, { ...params, currentPage, pageSize });
      if (res.code == "200") {
        yield put({ type: 'updateState', payload: { authorizeList: res.data.records, total: res.data.total }});
      }
      yield put({ type: "updateState", payload: { listLoading: false }});
    },

    *getMenuTree({ payload: params }, { select, put, call }) {
      const res = yield call(menuService.getMenuPage, params);
      yield put({type: "updateState", payload: {menuList: res.data.records}});
    },

    *onAddAuthorize({payload: params}, { select, call, put }) {
      yield put({ type: "updateState", payload: { listLoading: true }});
      const res = yield call(authorizeService.addAuthorize, { ...params });
      if (res.code == "200") {
        delete params.subjectCode;
        yield put({ type: 'getAuthorizePage', payload: { ...params }});
      }
    },

    *getAppRelateMenuTree({ payload: params }, { select, call, put }) {
      yield put({ type: "updateState", payload: { listLoading: true }});
      const res = yield call(authorizeService.getAppRelateMenuTree, { ...params });
      if (res.code == "200") {
        yield put({type: "updateState", payload: { menuList: res.data }});
      }
    },

    *deleteAuthorize({ payload: params }, { select, call, put }) {
      const res = yield call(authorizeService.deleteAuthorize, { ...params });
      if (res.code == "200") {
        message.success("删除成功");
        yield put({ type: 'getAuthorizePage', payload: { ...params }});
      } else {
        message.success("删除失败");
      }
    },

    *getMenuPage({payload: params}, {select, put, call}) {
      const res = yield call(roleService.getMenuPage, {...params, currentPage: 1, pageSize: 999999});
      yield put({type: "updateState", payload: {menuList: res.data.records}});
    }
  },

  subscriptions: {
    setup({ dispatch, history }) {
      const pathname = window.location.pathname;
      console.log("authorizeModel subscriptions setup pathname => ", pathname);
      // 应用授权
      if (pathname === "/limit/associate") {
        dispatch({ type: 'initApp', payload: {}});
      }

      // 角色授权
      if (pathname === "/limit/accredit") {
        dispatch({ type: 'initRole', payload: {}});
      }

      // 岗位授权
      if (pathname === "/limit/authorize") {
        dispatch({ type: 'initPost', payload: {}});
      }
    },
  },
}
